package com.riadeveloper.tile
{
	import com.greensock.TweenMax;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	public class ImageLoader extends Sprite
	{
		private var loader:Loader;
		private var grayMc:Sprite;
		private var callback:Function;
		private var position:String;
		
		public function ImageLoader(src:String,_width:Number,_height:Number,_position:String,_callback:Function = null)
		{

			callback = _callback;
			position = _position;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded);
			loader.load(new URLRequest(src))
			
			grayMc = new Sprite();
			grayMc.graphics.beginFill(0xcccccc,1);
			grayMc.graphics.drawRect(0,0,_width,_height);
			grayMc.graphics.endFill();
			addChild(grayMc);
			grayMc.scaleX = 0;
			grayMc.scaleY = 0;
			
			
			
		}
		private function  error(e:IOErrorEvent):void
		{
			
		}
		private function loading(e:ProgressEvent):void
		{
			var per:Number = e.bytesLoaded/e.bytesTotal;
			switch(position.charAt(0))
			{
				case "T":
					
					break;
				case "R":
					grayMc.scaleX = 1;
					grayMc.scaleY = per;
					break;
				case "B":
					
					break;
				case "L":
					grayMc.scaleX = per;
					grayMc.scaleY = 1;
					break;
			}
			
		}
		private function loaded(e:Event):void
		{
			TweenMax.to(grayMc,.5,{scaleX:0,onComplete:callback});
			addChildAt(loader.content,0)
		}
	}
}